/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import dao.DanhmucDAO;
import dao.SanphamDAO;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import pojo.Danhmuc;
import pojo.Loaisp;
import pojo.Sanpham;

/**
 *
 * @author kydrenw
 */
public class BangGiaController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.reset();
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=banggia.xls");

        HSSFWorkbook wb = new HSSFWorkbook();
        List<Danhmuc> dsdm = DanhmucDAO.layDanhSach();
        for (int i = 0; i < dsdm.size(); i++) {
            Danhmuc dm = dsdm.get(i);
            HSSFSheet sheet = wb.createSheet(dm.getTen());
            sheet.setAutobreaks(true);
            int dong = 0;
            HSSFRow header = sheet.createRow(dong++);
            HSSFCellStyle cellStyle = wb.createCellStyle();
            cellStyle.setFillBackgroundColor(HSSFColor.GREY_50_PERCENT.index);
            cellStyle.setFillForegroundColor(HSSFColor.WHITE.index);
            header.createCell(0).setCellValue("STT");
            header.createCell(1).setCellValue("Mã sản phẩm");
            header.createCell(2).setCellValue("Tên sản phẩm");
            header.createCell(3).setCellValue("Loại sản phẩm");
            header.createCell(4).setCellValue("Giá");

            List<Loaisp> dsloai = new ArrayList<Loaisp>(dm.getLoaisps());
            
            for (int j = 0; j < dsloai.size(); j++) {
                Loaisp loai = dsloai.get(j);
                List<Sanpham> dssp = SanphamDAO.laySanPhamTheoLoai(loai.getMa(), -1, -1);
                for (int m = 0; m < dssp.size(); m++) {
                    Sanpham sp = dssp.get(m);
                    HSSFRow row = sheet.createRow(dong++);
                    row.createCell(0).setCellValue(dong - 1);
                    row.createCell(1).setCellValue(sp.getMasp());
                    row.createCell(2).setCellValue(sp.getTensp());
                    row.createCell(3).setCellValue(sp.getLoaisp().getTen());
                    row.createCell(4).setCellValue(sp.getGia());
                }
            }
        }


        ServletOutputStream out = response.getOutputStream();
        wb.write(out);
        out.close();

        return null;
    }
}
